(function () {

	'use strict';

	angular
		.module('app')
		.controller('ProfileController', ProfileController);

	ProfileController.$inject = ['$scope', '$http', '$rootScope', '$location', '$state', 'API', 'AppAuthenticationService', 'CartModel', 'ConfigModel'];

	function ProfileController($scope, $http, $rootScope, $location, $state, API, AppAuthenticationService, CartModel, ConfigModel) {

		$scope.touchAllOrders = function () {
            $state.go('my-order', {
                tab: 'all'
            });
        };

		$scope.touchOrderCreated = function () {
            $state.go('my-order', {
                tab: 'created'
            });
        };

		$scope.touchOrderPayed = function () {
            $state.go('my-order', {
                tab: 'paid'
            });
        };

		$scope.touchOrderDelivering = function () {
            $state.go('my-order', {
                tab: 'delivering'
            });
        };

		$scope.touchOrderDelivered = function () {
            $state.go('my-order', {
                tab: 'delivered'
            });
        };

		$scope.touchFav = function () {
            $state.go('my-favorite', {});
        };

		$scope.touchAddress = function () {
            $state.go('my-address', {});
        };

		$scope.touchScore = function () {
            $state.go('my-score', {
                tab: 'all'
            });
        };

		$scope.touchCard = function () {
            $state.go('my-card', {
                tab: 'all'
            });
        };

		$scope.touchCashGift = function () {
            $state.go('my-cashgift', {});
        };

		$scope.touchHelps = function () {
            $state.go('article', {});
        };

		$scope.touchPassword = function () {
            $state.go('change-password', {});
        };

		$scope.touchSignin = function () {
            if (!AppAuthenticationService.getToken()) {
                $scope.goSignin();
            }
        };

		$scope.touchSignout = function () {
            if (AppAuthenticationService.getToken()) {
                API.auth.base.signout().then(function (success) {
                    if (success) {
                        $scope.goHome();
                        $scope.toast('注销成功');
                    } else {
                        $scope.toast('注销失败');
                    }
                });
            }
        };

		$scope.touchBonus = function (){
            $state.go('bonus', {});
        };

		$scope.touchBalance = function (){
            $state.go('my-balance', {});
        };

		$scope.isSignIn = _isSignIn;

		$scope.isWeixin = function () {
            return $rootScope.isWeixin();
        };

		$scope.cartModel = CartModel;

		$scope.user = AppAuthenticationService.getUser();
		$scope.userName = "";

		// ConfigModel.fetch();
		// var config = ConfigModel.getConfig();
		// $scope.authorize = config.authorize;

		function _reloadUser() {
			if ( _isSignIn() ) {
				API.user.profileGet().then(function (res) {
					AppAuthenticationService.setUser(res);
					$scope.user = res;
					if(res.nickname){
						$scope.userName = res.nickname;
					} else {
                        $scope.userName = res.username;
					}
				})
			};
		}

		function _reload() {
			_reloadUser();
			$scope.cartModel.reloadIfNeeded();
		}

		function _isSignIn(){
			return AppAuthenticationService.getToken()
		}


		_reload();
	}

})();